<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Node.js-ApiDemo</title>
</head>
<body>
    <div id="box">看不到看不到</div>
    <button onclick="success()">我是按钮</button>
    <script>
    function success(){
        //步骤一:创建异步对象
        var http= new XMLHttpRequest();
        
        //步骤二:设置请求的url参数,参数一是请求的类型,参数二是请求的url,可以带参数
        http.open('GET', 'http://127.0.0.1:8080/data');
        //步骤三:发送请求
        http.send();
        //步骤四:注册事件 onreadystatechange 状态改变就会调用
        http.onreadystatechange= function(){
            if(http.status == 200 && http.readyState == 4){
                var msg= http.responseText;
                var box= document.getElementById('box');
                box.innerHTML= JSON.parse(msg).name;
            }
        }
    }
    </script>
</body>
</html>